//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类;

public class demo_904 {
    public demo_904() {
    }

    public static void main(String[] args) {
        System.out.println(totalFruit(new int[]{1, 2, 1}));
    }

    public static int totalFruit(int[] fruits) {
        int[] fruitClass = new int[]{-1, -1};
        int left = 0;
        int right = 0;

        int count;
        for(count = 0; right < fruits.length; ++right) {
            if (right < 2) {
                fruitClass[right] = fruits[right];
            } else if (fruits[right] != fruitClass[0] && fruits[right] != fruitClass[1]) {
                fruitClass[0] = fruits[right - 1];
                fruitClass[1] = fruits[right];

                for(int i = right - 2; i >= left; --i) {
                    if (fruits[i] != fruitClass[0] && fruits[i] != fruitClass[1]) {
                        left = i + 1;
                    }
                }
            }

            if (right - left + 1 > count) {
                count = right - left + 1;
            }
        }

        return count;
    }
}
